﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Web.Profile;
using System.IO;
using System.Text.RegularExpressions;
using System.Net;
using System.Drawing;
using PMG.model;
using PMG.data;
using PMG.data.Tool;
using Spring.Context;
using Spring.Context.Support;

/// <summary>
/// BasePage SoCool页面的基类
/// </summary>
public class AdminMenu : System.Web.UI.UserControl
{

    #region 管理员
    /// <summary>
    /// 当前管理员帐号
    /// </summary>
    public USER CurrAdmin
    {
        get
        {
            // 检查SESSION
            if (Session["adminUser"] != null)
                return (USER)Session["adminUser"];
            else
            {
                // 检查COOKIES
                // 帐号和过期时间
                if (Request.Cookies["adminId"] != null)
                {
                    int adminId = 0;
                    try
                    {
                        adminId = Convert.ToInt32(Request.Cookies["adminId"].Value);
                    }
                    catch { return null; }
                    // 获取
                    IApplicationContext ic = new XmlApplicationContext
                        (ParameterFactory.CfgFilePath
                        , ParameterFactory.CfgBusinessFilePath);
                    DaoUserSpring dao = (DaoUserSpring)ic.GetObject("DaoUserSpring");
                    USER admin = dao.LoadFromId(adminId);
                    if (admin != null)
                    {
                        Session["adminUser"] = admin;
                        return admin;
                    }
                    else
                        return null;
                }
                else
                    return null;
            }
        }
    }
    /// <summary>
    /// 登录用户，存储其项ID
    /// SESSION，COOKIE
    /// </summary>
    /// <param name="dtoUser"></param>
    /// <param name="remember">记住登录？</param>
    public void Loggin(USER admin, bool remember)
    {
        // 存入SESSION
        Session["adminUser"] = admin;
        if (remember)
        {
            // 存入COOKIE
            HttpCookie cookieAdminId = new HttpCookie("adminId", admin.Id.ToString());
            cookieAdminId.HttpOnly = true;
            cookieAdminId.Expires = DateTime.Now.AddDays(7);
            Response.Cookies.Add(cookieAdminId);
        }
    }
    /// <summary>
    /// 注销用户，退出登录
    /// </summary>
    public void Logout()
    {
        if (Session["adminUser"] != null)
            Session["adminUser"] = null;
        // 删除COOKIE
        HttpCookie cookie = Response.Cookies["adminId"];
        cookie.HttpOnly = true;
        if (cookie != null)
        {
            cookie.Expires = DateTime.Now.AddDays(-1);
            Response.Cookies.Add(cookie);
        }
    }
    #endregion
}
